<?php

/**
 * Menashop
 *
 * LICENSE
 *
 * This source file is subject to the license that is bundled
 * with this project in the file LICENSE.txt
 *
 * @category   Menashop
 * @package    Menashop_Content
 * @subpackage ParentCategories
 * @copyright  Copyright (c) 2011 Menashop UA Inc. (http://www.menashop.com.ua)
 * @license    LICENSE.txt
 * @version    1.0.0
 * @since      File available since Release 1.0.0
 */

/**
 * The SAX handler for parent categories
 * 
 * @category   Menashop
 * @package    Menashop_Content
 * @subpackage ParentCategories
 * @copyright  Copyright (c) 2011 Menashop UA Inc. (http://www.menashop.com.ua)
 * @license    LICENSE.txt
 */
class Menashop_Content_ParentCategories
{

    /**
     * The data model for a category
     * @var Admin_Model_CategoryModel
     */
    protected $_categoryModel = null;

    /**
     * Category ID
     * @var integer
     */
    protected $_categoryId = null;

    /**
     * The counter for parent categories
     * @var integer 
     */
    protected $_totalParentCategories = 0;

    /**
     * The counter for updated parent categories
     * @var integer 
     */
    protected $_totalParentCategoriesUpdated = 0;

    /**
     * The counter for updated categories
     * @var integer 
     */
    protected $_totalCategoriesUpdated = 0;

    /**
     * The hash for processed parent categories
     * @var integer 
     */
    protected $_processedParentCategories = array();

    /**
     * Initialize the SAX handler for parent categories
     */
    public function __construct()
    {
        $this->_categoryModel = new Admin_Model_Category();
    }

    /**
     * Process the start of an element in XML
     * @param obejct $parser
     * @param string $tag
     * @param array $attributes 
     */
    public function startElement($parser, $tag, $attributes)
    {
        if ('Category' == $tag) {
            $this->_categoryId = $this->_categoryModel->findIdByContentId($attributes['ID']);
        } elseif ('ParentCategory' == $tag) {
            // try to get parent category from the hash
            if (!isset($this->_processedParentCategories[$attributes['ID']])) {
                $parentCategoryId = $this->_categoryModel->findIdByContentId($attributes['ID']);
                if ($parentCategoryId) {
                    // update a parent category
                    $parentCategory = array();
                    $parentCategory['id'] = $parentCategoryId;
                    $parentCategory['has_children'] = 1;
                    $this->_categoryModel->save($parentCategory);
                    $this->_processedParentCategories[$attributes['ID']] = $parentCategoryId;
                    $this->_totalParentCategoriesUpdated++;
                }
            }
            // check parent category
            if (isset($this->_processedParentCategories[$attributes['ID']])) {
                if ($this->_categoryId) {
                    // update a child category
                    $category = array();
                    $category['id'] = $this->_categoryId;
                    $category['parent_id'] = $this->_processedParentCategories[$attributes['ID']];
                    $this->_categoryModel->save($category);
                    $this->_totalCategoriesUpdated++;
                }
            }

            $this->_totalParentCategories++;
        }
    }

    /**
     * Process the end of an element in XML
     * @param object $parser
     * @param string $tag 
     */
    public function endElement($parser, $tag)
    {
        
    }

    /**
     * Process characters in XML
     * @param object $parser
     * @param string $data 
     */
    public function characterData($parser, $data)
    {
        
    }

    /**
     * Get the report about a parsing
     * @return string 
     */
    public function getReport()
    {
        $report = "$this->_totalParentCategories parent categories in XML,";
        $report .= " $this->_totalParentCategoriesUpdated parent categories updated,";
        $report .= " $this->_totalCategoriesUpdated children categories updated";
        return $report;
    }

}

